import 'dart:collection';

import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:opene/app_constant.dart';
import 'package:opene/app_theme.dart';
import 'package:opene/app_utils.dart';
import 'package:opene/net/network_manager.dart';
import 'package:opene/net/response/base_response.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:toast/toast.dart';

import '../../base/base_app_bar.dart';
import '../../base/scan_code.dart';
import '../../qr_scan.dart';

class Search extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SearchPage();
  }
}

class SearchPage extends StatefulWidget {
  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  // 创建TextEditingController
  final TextEditingController _controller = TextEditingController();
  final _formKey = GlobalKey<FormState>();
  String hint = "";
  String code;
  int type = 1;

  bool layout_title = false;
  String tv_title1 = "";
  String tv_title2 = "";

  bool layout_title_sku = false;
  String tv_sku_total = "";
  String tv_sku_lpnqty = "";

  bool layout_label_content = false;
  bool tv_confirm_label = false;
  String tv_printed_value = "";
  String tv_canceled_value = "";
  String tv_confirm_value = "";

  bool layout_trk = false;
  String tv_trk_time = "";
  String tv_trk_status = "";
  String tv_trk_sku = "";
  String tv_trk_lpn = "";

  SearchLabel searchLabel;
  List<SearchOther> searchOtherList;
  List<SearchContainer> searchContainerList;

  int _selectedRadioValue = 0;

  // 下拉菜单更新选择
  void _updateHint(String text) {
    setState(() {
      hint = text;
    });
  }

  void _updateRadio(int value) {
    setState(() {
      code = "";
      _controller.text = code;
      hint = "";
      this.type = value;
      layout_title = false;
      layout_title_sku = false;
      layout_label_content = false;
      tv_confirm_label = false;
      searchOtherList = null;
      searchContainerList = null;
      layout_trk = false;
    });
  }

  @override
  void dispose() {
    // 清理控制器
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: CommonAppBar(
          title: 'Search',
          leading: IconButton(
            icon: Icon(Icons.arrow_back_ios),
            onPressed: () {
              Navigator.of(context).pop(true);
            },
          ),
        ),
        body: Container(
          color: MyAppTheme.white,
          child: Padding(
            padding: const EdgeInsets.only(left: 15, top: 10, right: 15),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                ConstrainedBox(
                  constraints: BoxConstraints(maxHeight: 40),
                  child: TextField(
                    controller: _controller,
                    textInputAction: TextInputAction.search,
                    onSubmitted: (String text) {
                      print('Searching for $text');
                      if (text == null) {
                        return;
                      }
                      getSearch(text);
                    },
                    // autofocus: true,
                    decoration: InputDecoration(
                      suffixIcon: IconButton(
                        icon: Icon(
                          Icons.qr_code_scanner,
                          size: 25,
                          color: MyAppTheme.nearlyBlue,
                        ),
                        onPressed: () {
                          // requestPermissions();
                          // Utils.scanBarcode(context);
                          Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                            return ScanCode();
                          })).then((text) {
                            if (text == null) {
                              return;
                            }
                            print(text); //拿到扫描结果
                            getSearch(text);
                          });
                        },
                      ),
                      border: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: MyAppTheme.nearlyBlue,
                        ),
                        borderRadius: const BorderRadius.all(Radius.circular(20.0)),
                      ),
                      enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: MyAppTheme.nearlyBlue),
                        // 设置焦点之外的边框颜色
                        borderRadius: const BorderRadius.all(Radius.circular(20.0)),
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: MyAppTheme.nearlyBlue),
                        // 设置焦点时的边框颜色
                        borderRadius: const BorderRadius.all(Radius.circular(20.0)),
                      ),
                      hintText: "Please scan or input",
                      filled: true,
                      fillColor: MyAppTheme.white,
                      //隐藏下划线
                      //border: InputBorder.none,
                      hintStyle: TextStyle(color: Colors.grey),
                      contentPadding: const EdgeInsets.symmetric(vertical: 0, horizontal: 15),
                    ),
                    onChanged: (text) {
                      if (text == null) {
                        code = text;
                        return;
                      }
                      if ((code != null) && (code == text)) {
                        return;
                      }
                      code = text;
                    },
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 10),
                ),
                Row(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Expanded(
                      flex: 1, //设置权重
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                            padding: EdgeInsets.zero,
                            margin: EdgeInsets.zero,
                            height: 25.0,
                            child: Radio<int>(
                              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              value: 1,
                              groupValue: this.type,
                              activeColor: MyAppTheme.nearlyBlue,
                              onChanged: (value) {
                                _updateRadio(value);
                              },
                            ),
                          ),
                          GestureDetector(
                            child: Text(
                              "Location",
                              style: TextStyle(color: this.type == 1 ? MyAppTheme.nearlyBlue : MyAppTheme.color_333333, fontSize: 14.0),
                            ),
                            onTap: () {
                              _updateRadio(1);
                            },
                          ),
                        ],
                      ),
                    ),
                    Expanded(
                      flex: 1, //设置权重
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                            padding: EdgeInsets.zero,
                            margin: EdgeInsets.zero,
                            height: 25.0,
                            child: Radio<int>(
                              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              value: 2,
                              groupValue: this.type,
                              activeColor: MyAppTheme.nearlyBlue,
                              onChanged: (value) {
                                _updateRadio(value);
                              },
                            ),
                          ),
                          GestureDetector(
                            child: Text(
                              "IBLPN",
                              style: TextStyle(color: this.type == 2 ? MyAppTheme.nearlyBlue : MyAppTheme.color_333333, fontSize: 14.0),
                            ),
                            onTap: () {
                              _updateRadio(2);
                            },
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                Row(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Expanded(
                      flex: 1, //设置权重
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                            padding: EdgeInsets.zero,
                            margin: EdgeInsets.zero,
                            height: 25.0,
                            child: Radio<int>(
                              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              value: 3,
                              groupValue: this.type,
                              activeColor: MyAppTheme.nearlyBlue,
                              onChanged: (value) {
                                _updateRadio(value);
                              },
                            ),
                          ),
                          GestureDetector(
                            child: Text(
                              "Lable",
                              style: TextStyle(color: this.type == 3 ? MyAppTheme.nearlyBlue : MyAppTheme.color_333333, fontSize: 14.0),
                            ),
                            onTap: () {
                              _updateRadio(3);
                            },
                          ),
                        ],
                      ),
                    ),
                    Expanded(
                      flex: 1, //设置权重
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                            padding: EdgeInsets.zero,
                            margin: EdgeInsets.zero,
                            height: 25.0,
                            child: Radio<int>(
                              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              value: 4,
                              groupValue: this.type,
                              activeColor: MyAppTheme.nearlyBlue,
                              onChanged: (value) {
                                _updateRadio(value);
                              },
                            ),
                          ),
                          GestureDetector(
                            child: Text(
                              "SKU",
                              style: TextStyle(color: this.type == 4 ? MyAppTheme.nearlyBlue : MyAppTheme.color_333333, fontSize: 14.0),
                            ),
                            onTap: () {
                              _updateRadio(4);
                            },
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                Row(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Expanded(
                      flex: 1, //设置权重
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                            padding: EdgeInsets.zero,
                            margin: EdgeInsets.zero,
                            height: 25.0,
                            child: Radio<int>(
                              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              value: 5,
                              groupValue: this.type,
                              activeColor: MyAppTheme.nearlyBlue,
                              onChanged: (value) {
                                _updateRadio(value);
                              },
                            ),
                          ),
                          GestureDetector(
                            child: Text(
                              "Container",
                              style: TextStyle(color: this.type == 5 ? MyAppTheme.nearlyBlue : MyAppTheme.color_333333, fontSize: 14.0),
                            ),
                            onTap: () {
                              _updateRadio(5);
                            },
                          ),
                        ],
                      ),
                    ),
                    Expanded(
                      flex: 1, //设置权重
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                            padding: EdgeInsets.zero,
                            margin: EdgeInsets.zero,
                            height: 25.0,
                            child: Radio<int>(
                              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              value: 6,
                              groupValue: this.type,
                              activeColor: MyAppTheme.nearlyBlue,
                              onChanged: (value) {
                                _updateRadio(value);
                              },
                            ),
                          ),
                          GestureDetector(
                            child: Text(
                              "cmsContainer",
                              style: TextStyle(color: this.type == 6 ? MyAppTheme.nearlyBlue : MyAppTheme.color_333333, fontSize: 14.0),
                            ),
                            onTap: () {
                              _updateRadio(6);
                            },
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                Row(
                  children: <Widget>[
                    Expanded(
                      flex: 1, //设置权重
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                            padding: EdgeInsets.zero,
                            margin: EdgeInsets.zero,
                            height: 25.0,
                            child: Radio<int>(
                              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              value: 7,
                              groupValue: this.type,
                              activeColor: MyAppTheme.nearlyBlue,
                              onChanged: (value) {
                                _updateRadio(value);
                              },
                            ),
                          ),
                          GestureDetector(
                            child: Text(
                              "trk#(returns)",
                              style: TextStyle(color: this.type == 7 ? MyAppTheme.nearlyBlue : MyAppTheme.color_333333, fontSize: 14.0),
                            ),
                            onTap: () {
                              _updateRadio(7);
                            },
                          ),
                        ],
                      ),
                    ),
                    Expanded(
                      flex: 1, //设置权重
                      child: Row(
                        children: [
                          Container(
                            padding: EdgeInsets.zero,
                            margin: EdgeInsets.zero,
                            height: 25.0,
                            child: Radio<int>(
                              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              value: 8,
                              groupValue: this.type,
                              activeColor: MyAppTheme.nearlyBlue,
                              onChanged: (value) {
                                _updateRadio(value);
                              },
                            ),
                          ),
                          GestureDetector(
                            child: Text(
                              "Location(FBA)",
                              style: TextStyle(color: this.type == 8 ? MyAppTheme.nearlyBlue : MyAppTheme.color_333333, fontSize: 14.0),
                            ),
                            onTap: () {
                              _updateRadio(8);
                            },
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                Container(
                  margin: const EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 0),
                  width: double.infinity,
                ),
                Visibility(
                  visible: layout_title,
                  maintainState: true,
                  child: Row(
                    children: <Widget>[
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            tv_title1,
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            tv_title2,
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                    ],
                  ),
                ),
                Visibility(
                  visible: layout_title_sku,
                  maintainState: true,
                  child: Row(
                    children: <Widget>[
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            tv_sku_total,
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            tv_sku_lpnqty,
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                    ],
                  ),
                ),

                Visibility(
                  visible: layout_label_content,
                  maintainState: true,
                  child: Row(
                    children: <Widget>[
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            'Printed',
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            'Canceled',
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            'Confirm',
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                    ],
                  ),
                ),
                Visibility(
                  visible: layout_label_content,
                  maintainState: true,
                  child: Row(
                    children: <Widget>[
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            tv_printed_value,
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            tv_canceled_value,
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            tv_confirm_value,
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                    ],
                  ),
                ),
                Visibility(
                  visible: tv_confirm_label,
                  maintainState: true,
                  child: SizedBox(
                    height: 40, // 高度
                    child: ElevatedButton(
                      child: Text('Confirm'),
                      style: ButtonStyle(
                        backgroundColor: MaterialStateProperty.all<Color>(MyAppTheme.nearlyBlue),
                        shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                          RoundedRectangleBorder(
                            borderRadius: const BorderRadius.all(Radius.circular(20.0)),
                          ),
                        ),
                      ),
                      onPressed: () {},
                    ),
                  ),
                ),

                Visibility(
                  visible: layout_trk,
                  maintainState: true,
                  child: Row(
                    children: <Widget>[
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            "Time: ",
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            tv_trk_time,
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                    ],
                  ),
                ),
                Visibility(
                  visible: layout_trk,
                  maintainState: true,
                  child: Row(
                    children: <Widget>[
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            "Status: ",
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            tv_trk_status,
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                    ],
                  ),
                ),
                Visibility(
                  visible: layout_trk,
                  maintainState: true,
                  child: Row(
                    children: <Widget>[
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            "SKU: ",
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            tv_trk_sku,
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                    ],
                  ),
                ),
                Visibility(
                  visible: layout_trk,
                  maintainState: true,
                  child: Row(
                    children: <Widget>[
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            "IBLPN: ",
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                      Expanded(
                          flex: 1, //设置权重
                          child: Text(
                            tv_trk_lpn,
                            style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                          )),
                    ],
                  ),
                ),

                Container(
                  margin: const EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 0),
                  width: double.infinity,
                ),

                Expanded(
                  child: type == 5 || type == 6 ? setListViewContainer() : setListView(),
                ),

                // Expanded(
                //   child: ListView.builder(
                //     itemCount: inventorys == null ? 0 : inventorys.length,
                //     itemBuilder: (context, index) {
                //       SearchOther searchOther = inventorys[index];
                //       print("index:${index}");
                //       return Expanded(child:  Column(
                //         children: [
                //           Row(
                //             children: [
                //               Visibility(
                //                 visible:
                //                 (searchOther.inboundLockedQty > 0 ||
                //                     searchOther.pickLock)
                //                     ? true
                //                     : false,
                //                 child: Image.asset(
                //                   searchOther.pickLock
                //                       ? 'assets/images/lock_orange.png'
                //                       : 'assets/images/lock_red.png',
                //                   width: 15,
                //                   height: 15,
                //                 ),
                //               ),
                //               Visibility(
                //                 visible:
                //                 (type == 1 || type == 2|| type == 4) ? true : false,
                //                 child: Expanded(
                //                     flex: 1, //设置权重
                //                     child: Text(
                //                       type == 1? (searchOther.inboundLockedQty > 0 ? (searchOther.sku + '(${searchOther.inboundLockedQty})') : (searchOther.sku + '(${searchOther.inventory})'))
                //                           : ((type == 2 || type == 4)  ? Utils.getStringisEmpty( searchOther.location)
                //                           : ""),
                //                       style: TextStyle(
                //                           color: MyAppTheme.color_333333,
                //                           fontSize: 12.0),
                //                     )),
                //               ),
                //               Visibility(
                //                 visible: true,
                //                 child: Expanded(
                //                     flex: 1, //设置权重
                //                     child: Text(
                //                       type == 1 ? searchOther.lpn
                //                           : ((type == 2 || type == 4) ? (searchOther.inboundLockedQty > 0 ? (searchOther.sku + '(${searchOther.inboundLockedQty})') : (searchOther.sku + '(${searchOther.inventory})'))
                //                           : ""),
                //                       style: TextStyle(
                //                           color: MyAppTheme.color_333333,
                //                           fontSize: 12.0),
                //                     )),
                //               ),
                //             ],
                //           ),
                //           Visibility(
                //             visible: (searchOther.containerNos == null || searchOther.containerNos.length == 0) ?false:true,
                //             child: Row(
                //               children: [
                //                  Expanded(
                //                       flex: 1, //设置权重
                //                       child: Text("Container: ",
                //                         textAlign: TextAlign.right,
                //                         style: TextStyle(
                //                             color: MyAppTheme.color_333333,
                //                             fontSize: 10.0),
                //                       )
                //                  ),
                //                 Expanded(
                //                       flex: 1, //设置权重
                //                       child: ListView.builder(
                //                         shrinkWrap: true,
                //                         itemCount: searchOther.containerNos == null ? 0 : searchOther.containerNos.length,
                //                         itemBuilder: (context, index) {
                //                           return Text(searchOther.containerNos[index],
                //                             textAlign: TextAlign.left,
                //                             style: TextStyle(
                //                                 color: MyAppTheme.color_333333,
                //                                 fontSize: 10.0),
                //                           );
                //                         },
                //                       )
                //                 ),
                //               ],
                //             ),
                //           ),
                //           // Visibility(
                //           //   visible: searchOther.containerNos == null ?false:true,
                //           //   child: Container(
                //           //     margin: const EdgeInsets.only(
                //           //         left: 0, top: 5, right: 0, bottom: 0),
                //           //     child: ListView.builder(
                //           //       shrinkWrap: true,
                //           //       itemCount: searchOther.containerNos == null ? 0 : searchOther.containerNos.length,
                //           //       itemBuilder: (context, index) {
                //           //         return Text(searchOther.containerNos[index],
                //           //           style: TextStyle(
                //           //               color: MyAppTheme.color_333333,
                //           //               fontSize: 12.0),
                //           //         );
                //           //       },
                //           //     ),
                //           //   ),
                //           // ),
                //
                //           Divider(),
                //         ],
                //       ),);
                //     },
                //   ),
                // ),
                //
                // Expanded(
                //   child: ListView.builder(
                //     shrinkWrap: true,
                //     itemCount: searchContainerList == null ? 0 : searchContainerList.length,
                //     itemBuilder: (context, index) {
                //       SearchContainer searchContainer = searchContainerList[index];
                //       List<TallyInfo> tallyInfoRespVos = searchContainer.tallyInfoRespVos;
                //       if(type==5){
                //         tallyInfoRespVos = searchContainer.detail;
                //       }
                //       print("index:${index}");
                //       return Expanded(child:  Column(
                //         children: [
                //           Row(
                //             children: [
                //               Expanded(
                //                   flex: 1, //设置权重
                //                   child: Text( "Drop date:",
                //                     style: TextStyle(
                //                         color: MyAppTheme.color_666666,
                //                         fontSize: 12.0),
                //                   )),
                //               Expanded(
                //                   flex: 1, //设置权重
                //                   child: Text( Utils.getStringisEmpty(searchContainer.dropped),
                //                     style: TextStyle(
                //                         color: MyAppTheme.color_333333,
                //                         fontSize: 12.0),
                //                   )),
                //             ],
                //           ),
                //           Row(
                //             children: [
                //               Expanded(
                //                   flex: 1, //设置权重
                //                   child: Text( "Tally completed:",
                //                     style: TextStyle(
                //                         color: MyAppTheme.color_666666,
                //                         fontSize: 12.0),
                //                   )),
                //               Expanded(
                //                   flex: 1, //设置权重
                //                   child: Text( type==5?Utils.getStringisEmpty(searchContainer.tallyCompleted):Utils.getStringisEmpty(searchContainer.tallyCompleteDate),
                //                     style: TextStyle(
                //                         color: MyAppTheme.color_333333,
                //                         fontSize: 12.0),
                //                   )),
                //             ],
                //           ),
                //           Row(
                //             children: [
                //               Expanded(
                //                   flex: 1, //设置权重
                //                   child: Text( "IBLPN",
                //                     style: TextStyle(
                //                         color: MyAppTheme.color_666666,
                //                         fontSize: 12.0),
                //                   )),
                //               Expanded(
                //                   flex: 1, //设置权重
                //                   child: Text( type==5?'Location':'SKU[QTY]',
                //                     style: TextStyle(
                //                         color: MyAppTheme.color_333333,
                //                         fontSize: 12.0),
                //                   )),
                //             ],
                //           ),
                //           Visibility(
                //             visible: (tallyInfoRespVos == null || tallyInfoRespVos.length == 0) ?false:true,
                //             child: ListView.builder(
                //               shrinkWrap: true,
                //               itemCount: tallyInfoRespVos == null ? 0 : tallyInfoRespVos.length,
                //               itemBuilder: (context, index) {
                //                 return Row(
                //                   children: [
                //                     Expanded(
                //                         flex: 1, //设置权重
                //                         child: Text( Utils.getStringisEmpty(tallyInfoRespVos[index].lpn),
                //                           style: TextStyle(
                //                               color: MyAppTheme.color_666666,
                //                               fontSize: 12.0),
                //                         )),
                //                     Expanded(
                //                         flex: 1, //设置权重
                //                         child: Text(  type==5?Utils.getStringisEmpty(tallyInfoRespVos[index].wholeCode):Utils.getStringisEmpty(tallyInfoRespVos[index].skuAndQty),
                //                           style: TextStyle(
                //                               color: MyAppTheme.color_333333,
                //                               fontSize: 12.0),
                //                         )),
                //                   ],
                //                 );
                //               },
                //             ),
                //           ),
                //
                //           Divider(),
                //         ],
                //       ),);
                //     },
                //   ),
                // ),

                Container(
                  margin: const EdgeInsets.only(left: 0, top: 15, right: 0, bottom: 0),
                  width: double.infinity,
                ),
              ],
            ),
          ),
        ),
        bottomSheet: Container(
          width: double.infinity,
          padding: const EdgeInsets.only(left: 0, top: 2, right: 0),
          height: 20,
          child: Column(
            children: [
              Text(hint == null ? "" : hint, style: TextStyle(color: MyAppTheme.red, fontSize: 14.0)),
            ],
          ),
        ));
  }

  ListView setListView() {
    return ListView.builder(
      itemCount: searchOtherList == null ? 0 : searchOtherList.length,
      itemBuilder: (context, index) {
        SearchOther searchOther = searchOtherList[index];
        print("index:${index}");
        return Expanded(
          child: Column(
            children: [
              Row(
                children: [
                  Visibility(
                    visible: (searchOther.inboundLockedQty > 0 || searchOther.pickLock) ? true : false,
                    child: Image.asset(
                      searchOther.pickLock ? 'assets/images/lock_orange.png' : 'assets/images/lock_red.png',
                      width: 15,
                      height: 15,
                    ),
                  ),
                  Visibility(
                    visible: (type == 1 || type == 2 || type == 4) ? true : false,
                    child: Expanded(
                        flex: 1, //设置权重
                        child: Text(
                          type == 1 ? (searchOther.inboundLockedQty > 0 ? (searchOther.sku + '(${searchOther.inboundLockedQty})') : (searchOther.sku + '(${searchOther.inventory})')) : ((type == 2 || type == 4) ? Utils.getStringisEmpty(searchOther.location) : ""),
                          style: TextStyle(color: MyAppTheme.color_333333, fontSize: 12.0),
                        )),
                  ),
                  Visibility(
                    visible: true,
                    child: Expanded(
                        flex: 1, //设置权重
                        child: Text(
                          (type == 1 || type == 8) ? searchOther.lpn : ((type == 2 || type == 4) ? (searchOther.inboundLockedQty > 0 ? (searchOther.sku + '(${searchOther.inboundLockedQty})') : (searchOther.sku + '(${searchOther.inventory})')) : ""),
                          style: TextStyle(color: MyAppTheme.color_333333, fontSize: 12.0),
                        )),
                  ),
                ],
              ),
              Visibility(
                visible: (searchOther.containerNos == null || searchOther.containerNos.length == 0) ? false : true,
                child: Row(
                  children: [
                    Expanded(
                        flex: 1, //设置权重
                        child: Text(
                          "Container: ",
                          textAlign: TextAlign.right,
                          style: TextStyle(color: MyAppTheme.color_333333, fontSize: 10.0),
                        )),
                    Expanded(
                        flex: 1, //设置权重
                        child: ListView.builder(
                          shrinkWrap: true,
                          itemCount: searchOther.containerNos == null ? 0 : searchOther.containerNos.length,
                          itemBuilder: (context, index) {
                            return Text(
                              searchOther.containerNos[index],
                              textAlign: TextAlign.left,
                              style: TextStyle(color: MyAppTheme.color_333333, fontSize: 10.0),
                            );
                          },
                        )),
                  ],
                ),
              ),
              Divider(),
            ],
          ),
        );
      },
    );
  }

  ListView setListViewContainer() {
    return ListView.builder(
      shrinkWrap: true,
      itemCount: searchContainerList == null ? 0 : searchContainerList.length,
      itemBuilder: (context, index) {
        SearchContainer searchContainer = searchContainerList[index];
        List<TallyInfo> tallyInfoRespVos;
        if (type == 6) {
          tallyInfoRespVos = searchContainer.detail;
        } else {
          tallyInfoRespVos = searchContainer.tallyInfoRespVos;
        }
        print(searchContainer.toString());
        // print("setListViewContainer>index:${index}");
        return Expanded(
          child: Column(
            children: [
              Row(
                children: [
                  Expanded(
                      flex: 1, //设置权重
                      child: Text(
                        "Drop date:",
                        style: TextStyle(color: MyAppTheme.color_666666, fontSize: 12.0),
                      )),
                  Expanded(
                      flex: 1, //设置权重
                      child: Text(
                        type == 6 ? Utils.getStringisEmpty(searchContainer.dropped) : Utils.getStringisEmpty(searchContainer.dropDate),
                        style: TextStyle(color: MyAppTheme.color_333333, fontSize: 12.0),
                      )),
                ],
              ),
              Row(
                children: [
                  Expanded(
                      flex: 1, //设置权重
                      child: Text(
                        "Tally completed:",
                        style: TextStyle(color: MyAppTheme.color_666666, fontSize: 12.0),
                      )),
                  Expanded(
                      flex: 1, //设置权重
                      child: Text(
                        type == 6 ? Utils.getStringisEmpty(searchContainer.tallyCompleted) : Utils.getStringisEmpty(searchContainer.tallyCompleteDate),
                        style: TextStyle(color: MyAppTheme.color_333333, fontSize: 12.0),
                      )),
                ],
              ),
              Row(
                children: [
                  Expanded(
                      flex: 1, //设置权重
                      child: Text(
                        "IBLPN",
                        style: TextStyle(color: MyAppTheme.color_666666, fontSize: 12.0),
                      )),
                  Expanded(
                      flex: 1, //设置权重
                      child: Text(
                        type == 6 ? 'Location' : 'SKU[QTY]',
                        style: TextStyle(color: MyAppTheme.color_333333, fontSize: 12.0),
                      )),
                ],
              ),
              Visibility(
                visible: (tallyInfoRespVos == null || tallyInfoRespVos.length == 0) ? false : true,
                child: ListView.builder(
                  shrinkWrap: true,
                  itemCount: tallyInfoRespVos == null ? 0 : tallyInfoRespVos.length,
                  itemBuilder: (context, index) {
                    return Row(
                      children: [
                        Expanded(
                            flex: 1, //设置权重
                            child: Text(
                              Utils.getStringisEmpty(tallyInfoRespVos[index].lpn),
                              style: TextStyle(color: MyAppTheme.color_666666, fontSize: 12.0),
                            )),
                        Expanded(
                            flex: 1, //设置权重
                            child: Text(
                              type == 6 ? Utils.getStringisEmpty(tallyInfoRespVos[index].wholeCode) : Utils.getStringisEmpty(tallyInfoRespVos[index].skuAndQty),
                              style: TextStyle(color: MyAppTheme.color_333333, fontSize: 12.0),
                            )),
                      ],
                    );
                  },
                ),
              ),
              Divider(),
            ],
          ),
        );
      },
    );
  }

  void getSearch(String text) async {
    setState(() {
      code = text;
      _controller.text = code;
      hint = "";
    });
    String url = Constants.search;
    Map<String, dynamic> map = new HashMap();
    switch (type) {
      case 1:
        map['barcode'] = text;
        map['category'] = "location";
        break;
      case 2:
        map['barcode'] = text;
        map['category'] = "lpn";
        break;
      case 3:
        map['barcode'] = text;
        map['category'] = "label";
        break;
      case 4:
        map['barcode'] = text;
        map['category'] = "sku";
        break;
      case 5:
        url = Constants.searchContainerNo;
        map['containerNo'] = text;
        break;
      case 6:
        url = Constants.container + "/" + text;
//                map.put("containerNo", text);
        break;
      case 7:
        map['barcode'] = text;
        map['category'] = "trk-returns";
        break;
      case 8:
        url = Constants.cmsLocationGetLocation + "/" + text;
        break;
    }

    BaseResponse baseResponse = await NetworkManager().request(context, url, params: map);

    if (baseResponse.status == 200) {
      switch (type) {
        case 1: //locaoin
          SearchBean searchBean = SearchBean.fromJson(baseResponse.data);
          setState(() {
            layout_title = true;
            tv_title1 = "SKU(QTY)";
            tv_title2 = "IBLPN";
            searchOtherList = searchBean.inventory;
          });
          break;
        case 2: //iblpn
          SearchBean searchBean = SearchBean.fromJson(baseResponse.data);
          setState(() {
            layout_title = true;
            tv_title1 = "Location";
            tv_title2 = "SKU(QTY)";
            searchOtherList = searchBean.inventory;
          });
          break;
        case 3: //lable
          SearchBean searchBean = SearchBean.fromJson(baseResponse.data);
          setState(() {
            layout_label_content = true;
            searchLabel = searchBean.labelDetail;
            searchLabel.trackingCode = text;

            tv_printed_value = "${searchLabel.print}";
            tv_canceled_value = "${searchLabel.cancel}";
            tv_confirm_value = "${searchLabel.confirm}";

            if (searchLabel.print <= searchLabel.confirm + searchLabel.cancel) {
              tv_confirm_label = false;
            } else {
              tv_confirm_label = true;
            }
          });
          break;
        case 4: //sku
          SearchBean searchBean = SearchBean.fromJson(baseResponse.data);
          setState(() {
            layout_title = true;
            tv_title1 = "Location";
            tv_title2 = "SKU(QTY)";
            layout_title_sku = true;
            tv_sku_total = "Location";
            tv_sku_lpnqty = "SKU(QTY)";
            searchOtherList = searchBean.inventory;
            Map<String, String> map = HashMap();
            int total = 0;
            try {
              searchOtherList.forEach((searchOther) {
                total = total + searchOther.inventory;

                if ((searchOther.lpn != null) && (searchOther.lpn != "--")) {
                  map[searchOther.lpn] = searchOther.lpn;
                }
              });
            } catch (e) {
              e.printStackTrace();
            }
            tv_sku_total = 'Total: ${total}';
            tv_sku_lpnqty = 'LPN QTY: ${map.length}';
          });
          break;
        case 5: //container
        case 6: //cmscontainer
          searchContainerList = new List<dynamic>.from(baseResponse.data).map((userJson) => SearchContainer.fromJson(userJson)).toList();
          print(searchContainerList.length);
          break;
        case 7: //cmscontainer
          SearchBean searchBean = SearchBean.fromJson(baseResponse.data);
          var trkReturn = searchBean.trkReturn;
          if (trkReturn == null) {
            return;
          }
          setState(() {
            layout_trk = true;
            tv_trk_time = Utils.getStringisEmpty(trkReturn.time);
            tv_trk_status = Utils.getStringisEmpty(trkReturn.status);
            tv_trk_sku = Utils.getStringisEmpty(trkReturn.sku);
            tv_trk_lpn = Utils.getStringisEmpty(trkReturn.ibLpn);
          });
          break;
        case 7: //cmscontainer
          SearchFBATransfer fbaTransfer = SearchFBATransfer.fromJson(baseResponse.data);
          setState(() {
            if (fbaTransfer.lpns == null) {
              return;
            }
            if (searchOtherList == null) {
              searchOtherList = List();
            }
            searchOtherList.clear();
            fbaTransfer.lpns.forEach((value) {
              SearchOther searchOther = SearchOther(0, "", "", 0, 0, value, false, null);
              searchOtherList.add(searchOther);
            });
          });
          break;
      }
    } else {
      ToastContext().init(context);
      Toast.show(Utils.getStringisEmpty(baseResponse.msg), duration: Toast.lengthShort, gravity: Toast.bottom);
      _updateHint(baseResponse.msg);
    }
  }
}

class SearchBean {
  List<SearchOther> inventory;
  SearchLabel labelDetail;
  TrkReturnDTO trkReturn;

  SearchBean(this.inventory, this.labelDetail, this.trkReturn);

  SearchBean.fromJson(Map<dynamic, dynamic> json)
      : inventory = json['inventory'] == null ? null : (new List<dynamic>.from(json['inventory']).map((userJson) => SearchOther.fromJson(userJson)).toList()),
        labelDetail = json['descr'] == null ? null : SearchLabel.fromJson(json['descr']),
        trkReturn = json['trkReturn'] == null ? null : TrkReturnDTO.fromJson(json['trkReturn']);
}

class TrkReturnDTO {
  int id;
  String trk;
  String time;
  String status;
  String sku;
  String ibLpn;

  TrkReturnDTO(this.id, this.trk, this.time, this.status, this.sku, this.ibLpn);

  TrkReturnDTO.fromJson(Map<dynamic, dynamic> json)
      : id = json['id'] as int,
        trk = json['trk'] as String,
        time = json['time'] as String,
        status = json['status'] as String,
        sku = json['sku'] as String,
        ibLpn = json['ibLpn'] as String;
}

class SearchLabel {
  String trackingCode;
  String lpn;
  int print;
  int cancel;
  int confirm;
  String pickNo;
  String ibLpn;
  String location;
  String sku;

  SearchLabel(this.trackingCode, this.lpn, this.print, this.cancel, this.confirm, this.pickNo, this.ibLpn, this.location, this.sku);

  SearchLabel.fromJson(Map<dynamic, dynamic> json)
      : trackingCode = json['trackingCode'] as String,
        lpn = json['lpn'] as String,
        print = json['print'] as int,
        cancel = json['cancel'] as int,
        confirm = json['confirm'] as int,
        pickNo = json['pickNo'] as String,
        ibLpn = json['ibLpn'] as String,
        location = json['location'] as String,
        sku = json['sku'] as String;
}

class SearchOther {
  int id;
  String sku;
  String location;
  int inventory;
  int inboundLockedQty;
  String lpn;
  bool pickLock;
  List<String> containerNos;

  SearchOther(this.id, this.sku, this.location, this.inventory, this.inboundLockedQty, this.lpn, this.pickLock, this.containerNos);

  SearchOther.fromJson(Map<dynamic, dynamic> json)
      : id = json['id'] as int,
        sku = json['sku'] as String,
        location = json['location'] as String,
        inventory = json['inventory'] as int,
        inboundLockedQty = json['inboundLockedQty'] as int,
        lpn = json['lpn'] as String,
        pickLock = json['pickLock'] as bool,
        containerNos = json['containerNos'] == null ? null : new List<String>.from(json['containerNos']) as List<String>;
}

class SearchContainer {
  int id;
  String dropped;
  String dropDate;
  String tallyCompleted;
  String tallyCompleteDate;
  List<TallyInfo> tallyInfoRespVos;
  List<TallyInfo> detail;

  SearchContainer(this.id, this.dropped, this.dropDate, this.tallyCompleted, this.tallyCompleteDate, this.tallyInfoRespVos, this.detail);

  SearchContainer.fromJson(Map<dynamic, dynamic> json)
      : id = json['id'] as int,
        dropped = json['dropped'] as String,
        dropDate = json['dropDate'] as String,
        tallyCompleted = json['tallyCompleted'] as String,
        tallyCompleteDate = json['tallyCompleteDate'] as String,
        tallyInfoRespVos = json['tallyInfoRespVos'] == null ? null : (new List<dynamic>.from(json['tallyInfoRespVos']).map((userJson) => TallyInfo.fromJson(userJson)).toList()),
        detail = json['detail'] == null ? null : (new List<dynamic>.from(json['detail']).map((userJson) => TallyInfo.fromJson(userJson)).toList());

  @override
  String toString() {
    return 'SearchContainer{id: $id, dropped: $dropped, dropDate: $dropDate, tallyCompleted: $tallyCompleted, tallyCompleteDate: $tallyCompleteDate, tallyInfoRespVos: $tallyInfoRespVos, detail: $detail}';
  }
}

class TallyInfo {
  String lpn;
  String skuAndQty;
  String wholeCode;

  TallyInfo(this.lpn, this.skuAndQty, this.wholeCode);

  TallyInfo.fromJson(Map<dynamic, dynamic> json)
      : lpn = json['lpn'] as String,
        skuAndQty = json['skuAndQty'] as String,
        wholeCode = json['wholeCode'] as String;
}

class SearchFBATransfer {
  String location;
  List<String> lpns;

  SearchFBATransfer(this.location, this.lpns);

  SearchFBATransfer.fromJson(Map<dynamic, dynamic> json)
      : location = json['location'] as String,
        lpns = json['lpns'] == null ? null : new List<String>.from(json['containerNos']) as List<String>;
}

void requestPermissions() async {
  Map<Permission, PermissionStatus> statuses = await [
    Permission.location,
    Permission.storage,
    Permission.camera,
  ].request();

  print('位置权限：${statuses[Permission.location]}');
  print('存储权限：${statuses[Permission.storage]}');
}
